package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.HouseBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HouseActivity extends AppCompatActivity {
    ListView listView;
    HouseBeen houseBeen;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    HouseListView();
                    break;
            }
        }
    };

    private void HouseListView() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return houseBeen.rows.size();
            }

            @Override
            public Object getItem(int i) {
                return houseBeen.rows.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = LayoutInflater.from(HouseActivity.this).inflate(R.layout.item_house, null);
                ImageView imageView;
                imageView = view.findViewById(R.id.item_house_img);
                Glide.with(view).load(Configs.Host + houseBeen.rows.get(i).pic).into(imageView);
                TextView address;
                address = view.findViewById(R.id.item_house_address);
                address.setText(houseBeen.rows.get(i).sourceName);
                TextView price;
                price = view.findViewById(R.id.item_house_price);
                price.setText("面积：" + houseBeen.rows.get(i).price);
                TextView tel;
                tel = view.findViewById(R.id.item_house_tel);
                tel.setText("电话：" + houseBeen.rows.get(i).tel);
                return view;
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house);

        InitListView();
        LoadHouse();

    }
    private void InitListView() {
        listView = findViewById(R.id.house_listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(HouseActivity.this, HouseDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    private void LoadHouse() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.HouseList)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                houseBeen = new Gson().fromJson(response.body().string(), HouseBeen.class);
                handler.sendEmptyMessage(0);
            }
        });
    }
}